﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TIS.DAO;
using System.Data;

namespace TIS.Administrator
{
    public partial class ManagerNews : System.Web.UI.Page
    {
        LanguageDAO _languageDao = new LanguageDAO();
        NewsDAO _newsDao = new NewsDAO();
        CategoryDAO _categoryDao = new CategoryDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["langId"] == null)
                {
                    Session["langId"] = 1;
                }

                GetDataDdlLanguage();
                loadGridView();
            }
        }

        public void GetDataDdlLanguage()
        {

            ddlLanguage.DataSource = _languageDao.GetAllLanguage();
            ddlLanguage.DataTextField = "LanguageName";
            ddlLanguage.DataValueField = "LanguageId";
            ddlLanguage.SelectedValue = Session["langId"].ToString();
            ddlLanguage.DataBind();

            //DataRow emtyLanguage = dtLanguage.NewRow();
            //emtyLanguage["LanguageName"] = "Chọn ngôn ngữ bài viết";
            //emtyLanguage["LanguageId"] = "0";
            //dtLanguage.Rows.InsertAt(emtyLanguage, 0);
            ddlCategory.DataSource = _categoryDao.ListCategory(int.Parse(ddlLanguage.SelectedValue));
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryId";
            
          
           
            ddlCategory.DataBind();
            ddlCategory.Items.Add(new ListItem("Tất cả các danh mục", "0"));
            ddlCategory.SelectedValue = "0";
        }
        public void loadGridView()
        {
            gridViewNews.DataSource = _newsDao.GetNewsAll(int.Parse(ddlCategory.SelectedValue),int.Parse(Session["langId"].ToString()));
            gridViewNews.DataBind();
        }
        protected void btnShowLanguage_Click(object sender, EventArgs e)
        {
            Session["langId"] = ddlLanguage.SelectedValue;
            Response.Redirect("ManagerNews.aspx");
        }

        protected void lbtnRemove_Command(object sender, CommandEventArgs e)
        {
            _newsDao.DeleteNews(int.Parse(e.CommandArgument.ToString()));
            Response.Redirect("ManagerNews.aspx");
        }

        protected void lbtnEdit_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("AddNews.aspx?id=" + e.CommandArgument.ToString());
        }

        protected void gridViewNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridViewNews.PageIndex = e.NewPageIndex;
            loadGridView();
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadGridView();
        }

       
    }
}